package application;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Takes care of the info about seed creation received from createSeed.jsp
 * @author Aaryn
 */
public class CreateSeedServlet  extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * The constructor for the CreateSeedServlet
     */
    public CreateSeedServlet() {
    super();
}

    
    /**
     * method called for GET action
     * 
     * @param request 
     * @param response 
     * @throws ServletException
     * @throws IOException  
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    }
        
        
   /**
    * Takes the form info from createSeed.jsp and sends the info to the SeedDAO to
    * Put the new seed into the database. Afterwards, It takes you home to see your new 
    * seed if you pressed submit.
    * @param request 
    * @param response
    * @throws ServletException
    * @throws IOException  
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            HttpSession session = request.getSession(true);
            if(session.getAttribute("currentSessionUser") == null)
                response.sendRedirect("index.jsp?redirect=true");
            else{
            Seed seed = new Seed(request.getParameter("name"),request.getParameter("scientificName"), request.getParameter("growingTips"),
                    request.getParameter("specialRequirements"), request.getParameter("harvestingInformation"), request.getParameter("growingSeason"),
                    request.getParameter("quantity"),request.getParameter("climateZone"),
                    request.getParameter("hardinessNum")+request.getParameter("hardinessChar"), request.getParameter("price"));
            seed = SeedDAO.createSeed(seed, session);
            
            if(seed.getStatus().equals("seed added")){
                seed.setName(seed.getName().replaceAll("''", "'"));
                response.sendRedirect("profile.jsp?seedAdded="+seed.getName());
            }else{
                response.sendRedirect("createSeed.jsp?failReport="+seed.getStatus());
            }
            }
        } catch (Throwable exc){ 
            System.out.println(exc);
        }
}

}
